import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:flutter_plugin_printer/flutter_plugin_printer.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String mPrinterName = 'Unknown';
  String mPrinterImagePath = '打印路径';
  String mPrinterPat="2ujwdjkwedjk/3wj3j";
  @override
  void initState() {
    super.initState();
  }

  Future<void> searchForPrinter() async {
    String printerName;
    try {
      printerName = await FlutterPluginPrinter.searchForPrinter;
    } on PlatformException {
      printerName = 'Failed to get platform version.';
    }
    if (!mounted) return;
    setState(() {
      mPrinterName = printerName;
    });
  }


  Future<void> printImage(String  imagePath) async {
    String printerImagePath;
    try {
      printerImagePath = await FlutterPluginPrinter.printImage(imagePath);
    } on PlatformException {
      printerImagePath = 'Failed to get platform version.';
    }
    if (!mounted) return;
    setState(() {
      mPrinterImagePath = printerImagePath;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            children: <Widget>[
              Text('Running on: $mPrinterName\n'),
              FlatButton(child: Text("搜索"), onPressed: (){
                searchForPrinter();
              },),
              Text('Running on: $mPrinterImagePath\n'),
              FlatButton(child: Text("打印"),onPressed: (){
                printImage(mPrinterPat);
              },),
            ],
          ),
        ),
      ),
    );
  }
}
